package ioc;

import java.lang.management.BufferPoolMXBean;

/**
 * @Author：徐志荣
 * @Date：2023/6/3 下午 8:27
 *
 * ioc方法: 目的是解耦,
 * 整个对象的生命周期发生了改变
 *    上层不再依赖于下层
 *
 */
public class App {
    public static void main(String[] args) {
        Tire tire = new Tire(50,"green");
        Bottom bottom =new Bottom(tire);
        Framework framework  = new Framework(bottom);
        Car car = new Car(framework);
        car.init();
    }
}

class Car{
    Framework framework;
    public Car(Framework framework){
        this.framework = framework;
    }
    public void init(){
        System.out.println("汽车");
        framework.init();
    }
}

class Framework{
    private Bottom bottom;
    public Framework(Bottom bottom){
        this.bottom = bottom;
    }
    public void init(){
        System.out.println("车身");
        bottom.init();
    }
}
class Bottom{
   private Tire tire;
    //之前是: 实例化 tire, 现在是传一个 tire
    //让别人 提供一个 tire类

    public Bottom(Tire tire){
        this.tire = tire;
    }
    public void init(){
        System.out.println("底盘");
        tire.init();
    }
}
class Tire{
    private int size = 60;
    private String color;
    //添加一个参数的时候,上面的类不报错,因为 上面的类不依赖于 轮胎,而是依赖于 上层 的类 传进来的数据
    public  Tire(int size,String color){
        this.size = size;
        this.color = color;
    }
    public void init(){
        System.out.println("size:"+size + ",color:"+color);
    }
}

